import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import {config} from '../../shared/smartadmin.config';

declare var $: any;
@Component({
	selector: 'sa-car-statistics',
	templateUrl: './car-statistics.component.html',
})
export class CarStatisticsComponent implements OnInit {
	tableData:any[] = []; //table数据
	resultTableData:any[] = [] //客户端分页数据暂存
	totalCount:number = 0; //分页总条数
	curPage:number = 1; //分页当前页
	pageSize:number = 10; //内置的分页每页条数
	searchKey:string = ''; //搜索keyword
	carList:any[] = []; //车辆数据集合
	deviceId:string = '';//设备ID
	carNumber:string = '';//车牌号码
	startTime:string = '';//开始时间
	endTime:string = '';	//结束时间
	selectedCompanyName:string = ''; //公司树选中公司名称
	selectedCompanyId:string = '';//公司树选中公司ID
	
	constructor(private jsonApiService: JsonApiService) {}
	//刷新
	refresh(){
		this.load();
	}
	ngOnInit(){
		this.initDate().then(()=>this.load());//初始化日期,加载table表格数据
	}
	ngOnDestroy(){
		document.onclick = null;
	}
	//公司选中回调
	companySelected(data){
		this.selectedCompanyId = data.companyId;
		this.selectedCompanyName = data.companyName;
		this.deviceId = data.deviceId;
		this.carNumber = data.carNumber;
	}
	//导出汇总
	export_totle(){
		this.startTime =  $('#datetimepicker1').val();
		this.endTime =  $('#datetimepicker2').val();
		if(this.startTime==''||this.endTime==''){
			this.jsonApiService.message("起始结束时间不能为空");
			return;
		}
		let timeCheck=common.correctTimeParams(this.startTime,this.endTime,0);
		if(!timeCheck.status){
			this.jsonApiService.message(timeCheck.message);
			return;
		}
		var type = {
			name:"车辆汇总-"+this.startTime+'至'+this.endTime,
			num:"1"
		}
		this.createForm(type);
	}
	//导出明细
	export_datail(){
		this.startTime =  $('#datetimepicker1').val();
		this.endTime =  $('#datetimepicker2').val();
		if(this.startTime==''||this.endTime==''){
			this.jsonApiService.message("起始结束时间不能为空");
			return;
		}
		let timeCheck=common.correctTimeParams(this.startTime,this.endTime,0);
		if(!timeCheck.status){
			this.jsonApiService.message(timeCheck.message);
			return;
		}
		var type = {
			name:"车辆明细-"+this.startTime+'至'+this.endTime,
			num:"2"
		}
		this.createForm(type);
	}
	//导出表格
	createForm(type){
		this.startTime =  $('#datetimepicker1').val();
		this.endTime =  $('#datetimepicker2').val();
		var temdate = new Date();
		var nowTime = temdate.getTime().toString();
		var parameters = {
			deviceId:this.deviceId,
			startTime:this.startTime,
			endTime:this.endTime,
			fileName:type.name+nowTime+'',
			statsType:type.num,
			dataType:"1",
			searchKey:this.searchKey
		};	
		common.exprotDataFile(parameters, 'param', config.BaseURL + 'stats/weightlistExport.html')
	}
	getTime(date){
		var year = date.getFullYear();
		var month = (date.getMonth() + 1) < 10 ? "0" + (date.getMonth() + 1) : (date.getMonth() + 1);
		var day = date.getDate()  < 10 ? "0" + date.getDate() : date.getDate();
		return year+"-"+month+"-"+day;
	}
	//初始化日期
	initDate(){
		var that = this;
		return new Promise(function (resolve, reject) {
			System.import('assets/js/datepicker/js/bootstrap-datetimepicker.min.js').then(()=>{
					System.import('assets/js/datepicker/locales/bootstrap-datetimepicker.zh-CN.js').then(()=>{
						$("#datetimepicker1").datetimepicker({//时间插件
							format: "yyyy-mm-dd",
						    language: "zh-CN",
						    autoclose: true,
						    minView : 2
						}).on("changeDate", function(e){
							var date = e.date;
							if(!date){return;}
							var time = that.getTime(date);
							$("#datetimepicker2").datetimepicker("setStartDate", time);
						});
						$("#datetimepicker2").datetimepicker({//时间插件
							format: "yyyy-mm-dd",
						    language: "zh-CN",
						    autoclose: true,
						    minView : 2
						}).on("changeDate", function(e){
							var date = e.date;
							if(!date){return;}
							var time = that.getTime(date);
							$("#datetimepicker1").datetimepicker("setEndDate", time);
						});
						var date = new Date();
						$("#datetimepicker2").val(that.getTime(date));//当前时间
						date.setDate(date.getDate() - 7);//设置前一周
						$("#datetimepicker1").val(that.getTime(date));
						resolve();
					});
				}
			);
		})
	}
	//查询
	table_search(){
		this.curPage = 1;
		this.searchKey = common.stringTrim(this.searchKey);
		this.load();
	}
	//加载list数据
	load() {
		this.startTime =  $('#datetimepicker1').val();
		this.endTime =  $('#datetimepicker2').val();
		if(this.startTime==''||this.endTime==''){
			this.jsonApiService.message("起始结束时间不能为空");
			return;
		}
		let timeCheck=common.correctTimeParams(this.startTime,this.endTime,0);
		if(!timeCheck.status){
			this.jsonApiService.message(timeCheck.message);
			return;
		}
		let reqdata = 'gridPager={"parameters":{"deviceId":"'+this.deviceId+'","startTime":"'+this.startTime+'","endTime":"'+this.endTime+'","searchKey":"'+this.searchKey+'","dataType":"1"}}'
		this.jsonApiService.http("stats/statfordetailpage.html", reqdata).subscribe(data => {
			this.tableData = data;
			this.totalCount = data.length;
			this.client_pages(0,this.pageSize);
		})
	}
	//删除所选时间
	del_date(){
		this.startTime = '';
		this.endTime ='';
		$("#datetimepicker1").val('').change();
		$("#datetimepicker2").val('').change();
		$("#datetimepicker2").datetimepicker("setStartDate", null);
		$("#datetimepicker1").datetimepicker("setEndDate", null);
	}
	client_pages(start,length){
		this.resultTableData = this.tableData.slice(start,start+length);
	}
	//分页回调
	paginate(e){
		this.curPage = e.page+1;
		this.pageSize = e.rows;
		this.client_pages(e.first,e.rows);
	}
}